package net.studou.verify;

public class CheckLength implements CheckChain {

	private CheckChain nextChain;

	private int min;

	private int max;
	
	private String message;

	public CheckLength(int min, int max, CheckChain nextChain, String message) {
		this.nextChain = nextChain;
		this.max = max;
		this.min = min;
		this.message = message;
	}

	public String check(String content) {
		if (isLengthError(content)) {
			return message;
		} else {
			if (nextChain != null) {
				return nextChain.check(content);
			} else {
				return null;
			}
		}
	}

	private boolean isLengthError(String content) {
		boolean rtn = false;
		int len = content.length();
		if (len < min || len > max) {
			rtn = true;
		}
		return rtn;
	}
}
